home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH2 / WormyFruit.cs < prev   
Text File  |  2006-05-30  |  4KB  |  121 lines

  1. // ========================================================================
  2. //  WormyFruit.cs
  3. //
  4. //  Buggy version of TwotyFruity. It has five known bugs in it.
  5. //  This program adds up the costs and quantities of selected fruit types
  6. //  and outputs the results to the display. This module is a variation
  7. //  of the the FruitLoopy.cs module designed to demonstrate how to use
  8. //  functions.
  9. // ========================================================================
  10.  
  11. function initializeFruit()
  12. // ------------------------------------------------------------------------
  13. //     Set the starting values for our fruit arrays, and the type
  14. //     indices
  15. //
  16. //     RETURNS: number of different types of fruit
  17. //
  18. // ------------------------------------------------------------------------
  19. {
  20.     numFruitTypes = 5; // so we know how many types are in our arrays
  21.     $bananaIdx=0;    // initilize the values of our index variables
  22.     $appleIdx=1;
  23.     $orangeIdx=2;
  24.     $mangoIdx=3;
  25.     $pearIdx=3;
  26.  
  27.     $names[$bananaIdx] = "bananas"; // initilize the fruit name values
  28.     $names[$appleIdx] = "apples";
  29.     $names[$orangeIdx] = "oranges";
  30.     $names[$mangoIdx] = "mangos";
  31.     $names[$pearIdx] = "pears";
  32.  
  33.     $cost[$bananaIdx] = 1.15; // initilize the price values
  34.     $cost[$appleIdx] = 0.55;
  35.     $cost[$orangeIdx] = 0.55;
  36.     $cost[$mangoIdx] = 1.90;
  37.     $cost[$pearIdx] = 0.68;
  38.  
  39.     $quantity[$bananaIdx] = 1; // initilize the quantity values
  40.     $quantity[$appleIdx]  = 3;
  41.     $quantity[$orangeIdx] = 4;
  42.     $quantity[$mangoIdx]  = 1;
  43.     $quantity[$pearIdx]   = 2;
  44.  
  45.     return(%numFruitTypes);
  46. }
  47.  
  48. function addEmUp(%numFruitTypes)
  49. // ------------------------------------------------------------------------
  50. //     Add all prices of different fruit types to get a full total cost
  51. //
  52. //     PARAMETERS: %numFruitTypes -the number of different fruit that are tracked
  53. //
  54. //     RETURNS: total cost of all fruit
  55. //
  56. // ------------------------------------------------------------------------
  57. {
  58.    %total = 0;
  59.    for (%index = 0; %index <= %numFruitTypes; %index++)
  60.    {
  61.       %total = %total + ($quantity[%index]*$cost[%index]);
  62.    }
  63.    return $total;
  64. }
  65.  
  66.  
  67. // ------------------------------------------------------------------------
  68. //  countEm
  69. //
  70. //     Add all quantities of different fruit types to get a full total
  71. //
  72. //     PARAMETERS: %numFruitTypes -the number of different fruit that are tracked
  73. //
  74. //     RETURNS: total of all fruit types
  75. //
  76. // ------------------------------------------------------------------------
  77. function countEm(%numFruitTypes)
  78. {
  79.    %total = 0;
  80.    for (%index = 0; %index <= %numFruitTypes; %index++)
  81.    {
  82.       %total = %total + $quantity[%index];
  83.    }
  84. }
  85.  
  86. function runWormyFruit()
  87. // ------------------------------------------------------------------------
  88. //     Entry point for program. This program adds up the costs
  89. //     and quantities of selected fruit types and outputs the results to
  90. //     the display. This program is a variation of the program FruitLoopy
  91. //
  92. // ------------------------------------------------------------------------
  93. {
  94.    //
  95.    // ----------------- Initialization ---------------------
  96.    //
  97.  
  98.    %numFruitTypes=InitializeFruit(); // set up fruit arrays and variables
  99.    %numFruit=0     // always a good idea to initialize *all* variables!
  100.    %totalCost=0;    // (even if we know we are going to change them later)
  101.  
  102.    //
  103.    // ----------------- Computation ---------------------
  104.    //
  105.  
  106.    // Display the known statistics of the fruit collection
  107.    for (%index = 0; %index < %numFruitTypes; %index++)
  108.    {
  109.    echo("Cost of " @ $names[%index] @ ":$" @ $cost[%index]);
  110.    echo("Number of " @ $names[%index] @ ":" @ $quantity[%index]);
  111.    }
  112.  
  113.    // count up all the pieces of fruit, and display that result
  114.    %numFruits = countEm(%numFruitTypes));
  115.    echo("Total pieces of Fruit:" @ %numFruit);
  116.  
  117.    // now calculate the total cost
  118.    %totalCost = addEmUp(%numFruitTypes);
  119.    echo("Total Price of Fruit:$" @ %totalCost);
  120. }
  121.